﻿using System.Collections.Generic;
using System.IO;
using Lucid.ESA.ClientDataSystem.Application.Contracts;
using Lucid.ESA.ClientDataSystem.Application.DataTransferObjects;
using Lucid.ESA.ClientDataSystem.Application.Requests;
using Lucid.ESA.ClientDataSystem.Application.Responses;
using Lucid.ESA.ClientDataSystem.Data.Common;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using StructureMap;

namespace Lucid.ESA.ClientDataSystem.IntegrationTests
{
    [TestClass]
    public class SiteServiceTest
    {
        [TestInitialize()]
        public void MyTestInitialize()
        {
            //Get a fresh copy of the database.
            if (File.Exists("ClientDataSystem.sdf"))
                File.Delete("ClientDataSystem.sdf");

            File.Copy("InitialClientDataSystem.sdf", "ClientDataSystem.sdf");
        }

        [TestCleanup()]
        public void MyTestCleanup()
        {
            //Ensure session is closed after each test.
            SessionProvider.CloseSession();
        }

        [TestMethod]
        public void CanGetAllSites()
        {
            ISiteService siteService = ObjectFactory.GetInstance<ISiteService>();
            Response response = siteService.GetAllSites();
            Assert.AreEqual(ResponseStatus.Success, response.Status);
            List<SiteDto> sites = (response as GetResponse<List<SiteDto>>).Value;
            Assert.AreEqual(4, sites.Count);
        }

        [TestMethod]
        public void CanGetSiteById()
        {
            ISiteService siteService = ObjectFactory.GetInstance<ISiteService>();
            Response response = siteService.GetSiteById(1L);
            Assert.AreEqual(ResponseStatus.Success, response.Status);
            SiteDto site = (response as GetResponse<SiteDto>).Value;
            Assert.AreEqual(1, site.Id.Value);
            Assert.AreEqual("6 Orange Street", site.AddressLine1);
            Assert.AreEqual("Greenville", site.AddressLine2);
            Assert.AreEqual("Whiteshire", site.AddressLine3);
            Assert.IsNull(site.AddressLine4);
            Assert.AreEqual("M1 1AA", site.PostCode);
            Assert.IsTrue(site.MainSite);
            Assert.AreEqual(1, site.Client.Id.Value);
            Assert.AreEqual("Indigo Industries", site.Client.Name);
        }

        [TestMethod]
        public void CanAddSite()
        {
            ISiteService siteService = ObjectFactory.GetInstance<ISiteService>();

            CreateSiteRequest request = new CreateSiteRequest()
            {
                AddressLine1 = "17 Black Lane",
                AddressLine2 = "Tealburgh",
                AddressLine3 = "Blueshire",
                PostCode = "M1 1AA",
                MainSite = false,
                ClientId = 2
            };

            Response response = siteService.CreateSite(request);
            Assert.AreEqual(ResponseStatus.Success, response.Status);
            CreateResponse createResponse = response as CreateResponse;
            Assert.IsNotNull(createResponse);
            Assert.AreNotEqual(0, createResponse.Id);

            //To ensure new session.
            SessionProvider.CloseSession();

            Response getResponse = siteService.GetSiteById(createResponse.Id);
            Assert.AreEqual(ResponseStatus.Success, response.Status);
            SiteDto site = (getResponse as GetResponse<SiteDto>).Value;
            Assert.AreEqual(createResponse.Id, site.Id.Value);
            Assert.AreEqual("17 Black Lane", site.AddressLine1);
            Assert.AreEqual("Tealburgh", site.AddressLine2);
            Assert.AreEqual("Blueshire", site.AddressLine3);
            Assert.IsNull(site.AddressLine4);
            Assert.AreEqual("M1 1AA", site.PostCode);
            Assert.IsFalse(site.MainSite);
            Assert.AreEqual(2, site.Client.Id.Value);
            Assert.AreEqual("Burgundy Corporation", site.Client.Name);
        }
    }
}
